package com.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.campus.common.utils.StringUtils;
import com.campus.mapper.ArticleMapper;
import com.campus.mapper.CategoryMapper;
import com.campus.mapper.UserMapper;
import com.campus.pojos.domain.ClientArticle;
import com.campus.pojos.domain.ClientArticleLove;
import com.campus.pojos.domain.ClientCategory;
import com.campus.pojos.domain.vo.ArticleNumByCategoryVo;
import com.campus.pojos.domain.vo.ArticleNumVo;
import com.campus.pojos.domain.vo.ArticleVo;
import com.campus.pojos.domain.vo.UserNumVo;
import com.campus.service.ArticleService;
import com.campus.service.StatisticService;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class StatisticServiceImpl implements StatisticService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 每日帖子数量统计
     *
     * @param beginTime
     * @param endTime
     * @return
     */
    @Override
    public ArticleNumVo getArticleNum(LocalDate beginTime, LocalDate endTime) {
        //计算要统计的日期并存入dateList
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(beginTime);

        while(!beginTime.equals(endTime)){
            beginTime = beginTime.plusDays(1);
            dateList.add(beginTime);
        }
        String dateStr = StringUtils.join(dateList, ",");

        //统计每天的帖子数量
        List<Integer> numList = new ArrayList<>();
        for (LocalDate localDate : dateList) {
            //转成LocalDateTime
            LocalDateTime stTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime edTime = LocalDateTime.of(localDate, LocalTime.MAX);
            //查询count
            HashMap map = new HashMap();
            map.put("begin", stTime);
            map.put("end", edTime);
            map.put("status", 1);
            Integer count = articleMapper.sumByMap(map);
            numList.add(count);
        }
        //转成str
        String numStr = StringUtils.join(numList, ",");

        return ArticleNumVo
                .builder()
                .articleNumList(numStr)
                .dateList(dateStr)
                .build();
    }

    /**
     * 统计前5点赞帖子
     * @return
     */
    @Override
    public List<ArticleVo> top5(LocalDate beginTime, LocalDate endTime) {

        LocalDateTime stTime= LocalDateTime.of(beginTime, LocalTime.MIN);
        LocalDateTime edTime= LocalDateTime.of(endTime, LocalTime.MAX);

        QueryWrapper<ClientArticle> qw = new QueryWrapper<>();
        qw.le("create_time", edTime);
        qw.ge("create_time", stTime);
        qw.orderByDesc("love");
        //只算审核通过的帖子
        qw.eq("status",1);
        List<ClientArticle> articles = articleMapper.selectList(qw);
        List<ArticleVo> articleVos = new ArrayList<>();
        for (int i = 0; i < Math.min(5, articles.size()); i++) {
            ClientArticle article = articles.get(i);
            ArticleVo articleVo = new ArticleVo();
            BeanUtils.copyProperties(article, articleVo);
            articleVos.add(articleVo);
        }
        return articleVos;
    }

    @Override
    public UserNumVo getUserNum(LocalDate beginTime, LocalDate endTime) {
        //计算要统计的日期并存入dateList
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(beginTime);

        while(!beginTime.equals(endTime)){
            beginTime = beginTime.plusDays(1);
            dateList.add(beginTime);
        }
        String dateStr = StringUtils.join(dateList, ",");

        //统计每天的帖子数量
        List<Integer> numList = new ArrayList<>();
        for (LocalDate localDate : dateList) {
            //转成LocalDateTime
            LocalDateTime stTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime edTime = LocalDateTime.of(localDate, LocalTime.MAX);
            //查询count
            HashMap map = new HashMap();
            map.put("begin", stTime);
            map.put("end", edTime);
            map.put("status", 1);
            Integer count = userMapper.sumByMap(map);
            numList.add(count);
        }
        //转成str
        String numStr = StringUtils.join(numList, ",");

        return UserNumVo
                .builder()
                .userNumList(numStr)
                .dateList(dateStr)
                .build();
    }

    /**
     * 统计各墙类型下帖子数量
     * @return
     */
    @Override
    public ArticleNumByCategoryVo getArtNumByCategory(LocalDate beginTime, LocalDate endTime) {
        //开始时间 结束时间
        LocalDateTime stTime= LocalDateTime.of(beginTime, LocalTime.MIN);
        LocalDateTime edTime= LocalDateTime.of(endTime, LocalTime.MAX);
        //获取墙分类
        List<ClientCategory> categories = categoryMapper.selectList(null);
        String categoryStr = categories
                .stream()
                .map(ClientCategory::getCategoryName)
                .collect(Collectors.joining(","));

        List<Integer> numList = new ArrayList<>();
        for (ClientCategory category : categories) {
            QueryWrapper<ClientArticle> qw = new QueryWrapper<>();

            qw.eq("category_id", category.getId());
            qw.le("create_time", edTime);
            qw.ge("create_time", stTime);
            //只统计审核通过的帖子
            qw.eq("status",1);
            List<ClientArticle> articles = articleMapper.selectList(qw);
            numList.add(articles.size());
        }
        String numStr = StringUtils.join(numList, ",");

        return ArticleNumByCategoryVo.builder()
                .categoryList(categoryStr)
                .articleNumList(numStr)
                .build();
    }
}
